26#include "spdlog/spdlog.h"
27#include "spdlog/async.h"
28#include "spdlog/sinks/stdout_color_sinks.h"
31#include "spdlog/fmt/ostr.h"
40namespace do_not_enter_where_the_wolf_lives
42#define __INTERNAL_WOLF_MAIN_LOGGER_NAME_ "wolf_main_console"
54 "{}{}{}{}{}{}{}{}{}{}",
55 "{}{}{}{}{}{}{}{}{}{}{}",
56 "{}{}{}{}{}{}{}{}{}{}{}{}",
57 "{}{}{}{}{}{}{}{}{}{}{}{}{}",
58 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
59 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
60 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
61 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
62 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
63 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
64 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
65 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
66 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
67 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
68 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
69 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
70 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
71 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
72 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
73 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
74 "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}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
88 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
89 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
90 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
91 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
92 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
93 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
94 "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}");
107 Logger(
const std::string& name);
111 Logger(std::string&& name);
136 template <
typename... Args>
137 void info(Args&&... args)
const;
141 template <
typename... Args>
142 void warn(Args&&... args)
const;
146 template <
typename... Args>
147 void error(Args&&... args)
const;
151 template <
typename... Args>
152 void debug(Args&&... args)
const;
156 template <
typename... Args>
157 void trace(Args&&... args)
const;
179 console_->set_level(spdlog::level::trace);
184 spdlog::init_thread_pool(4096, 1);
212 console_->set_level(spdlog::level::trace);
217 spdlog::init_thread_pool(4096, 1);
244template <
typename... Args>
247#if FMT_VERSION >= 90000
248 console_->info(std::get<
sizeof...(args) - 1>(
repeated_brace), fmt::streamed(std::forward<Args>(args))...);
254template <
typename... Args>
257#if FMT_VERSION >= 90000
258 console_->warn(std::get<
sizeof...(args) - 1>(
repeated_brace), fmt::streamed(std::forward<Args>(args))...);
264template <
typename... Args>
267#if FMT_VERSION >= 90000
268 console_->error(std::get<
sizeof...(args) - 1>(
repeated_brace), fmt::streamed(std::forward<Args>(args))...);
274template <
typename... Args>
277#if FMT_VERSION >= 90000
278 console_->debug(std::get<
sizeof...(args) - 1>(
repeated_brace), fmt::streamed(std::forward<Args>(args))...);
284template <
typename... Args>
287#if FMT_VERSION >= 90000
288 console_->trace(std::get<
sizeof...(args) - 1>(
repeated_brace), fmt::streamed(std::forward<Args>(args))...);
296 bool p2 = q_size % 2 == 0;
298 if (p2) spdlog::init_thread_pool(q_size, 1);
325 bool exists(
const std::string& name)
const
327 std::lock_guard<std::mutex> lock(
mut_);
335 std::lock_guard<std::mutex> lock(
mut_);
376inline bool isMemberFunctionOf(
const std::string& _function_signature,
const std::string& _class_name)
379 const std::string name = _function_signature.substr(0, _function_signature.find(
'('));
380 const std::string scope =
"wolf::" + _class_name +
"::";
383 for (
auto pos = name.find(scope); pos != std::string::npos; pos = name.find(scope, pos + 1))
384 if (pos == 0 or name[pos - 1] ==
' ')
return true;
392#define WOLF_STRINGIFY(x) #x
393#define WOLF_STR_HELPER(x) WOLF_STRINGIFY(x)
399#if defined(__GNUC__) || defined(__clang__)
400#define WOLF_PRETTY_FUNCTION __PRETTY_FUNCTION__
401#elif defined(_MSC_VER)
402#define WOLF_PRETTY_FUNCTION __FUNCSIG__
420#define WOLF_ASYNC_QUEUE_LOG_NAMED(name, ...) \
421 wolf::internal::WolfLoggerManager::get().getLogger(name).set_async_queue(x);
423#define WOLF_INFO_NAMED(name, ...) \
425 char this_file[] = __FILE__; \
426 wolf::internal::WolfLoggerManager::get().getLogger(name).info( \
427 "[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
429#define WOLF_INFO_NAMED_COND(name, cond, ...) \
430 if (cond) WOLF_INFO_NAMED(name, __VA_ARGS__);
432#define WOLF_WARN_NAMED(name, ...) \
434 char this_file[] = __FILE__; \
435 wolf::internal::WolfLoggerManager::get().getLogger(name).warn( \
436 "[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
438#define WOLF_WARN_NAMED_COND(name, cond, ...) \
439 if (cond) WOLF_WARN_NAMED(name, __VA_ARGS__);
441#define WOLF_ERROR_NAMED(name, ...) \
443 char this_file[] = __FILE__; \
444 wolf::internal::WolfLoggerManager::get().getLogger(name).error( \
445 "[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
447#define WOLF_ERROR_NAMED_COND(name, cond, ...) \
448 if (cond) WOLF_ERROR_NAMED(name, __VA_ARGS__);
451#define WOLF_DEBUG_NAMED(name, ...) \
454 char this_file[] = __FILE__; \
455 wolf::internal::WolfLoggerManager::get().getLogger(name).debug( \
456 "[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
458#define WOLF_DEBUG_NAMED_COND(name, cond, ...) \
461 if (cond) WOLF_DEBUG_NAMED(name, __VA_ARGS__); \
464#define WOLF_DEBUG_NAMED(name, ...) \
468#define WOLF_DEBUG_NAMED_COND(name, cond, ...) \
475#define WOLF_TRACE_NAMED(name, ...) \
478 char this_file[] = __FILE__; \
479 wolf::internal::WolfLoggerManager::get().getLogger(name).trace( \
480 "[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
482#define WOLF_TRACE_NAMED_COND(name, cond, ...) \
485 if (cond) WOLF_TRACE_NAMED(name, __VA_ARGS__); \
488#define WOLF_TRACE_NAMED(name, ...) \
492#define WOLF_TRACE_NAMED_COND(name, cond, ...) \
500#define WOLF_ASYNC_QUEUE_LOG(x) wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_).set_async_queue(x);
502#define WOLF_INFO(...) \
504 char this_file[] = __FILE__; \
505 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
506 .info("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
508#define WOLF_INFO_COND(cond, ...) \
509 if (cond) WOLF_INFO(__VA_ARGS__);
511#define WOLF_WARN(...) \
513 char this_file[] = __FILE__; \
514 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
515 .warn("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
517#define WOLF_WARN_COND(cond, ...) \
518 if (cond) WOLF_WARN(__VA_ARGS__);
520#define WOLF_ERROR(...) \
522 char this_file[] = __FILE__; \
523 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
524 .error("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
526#define WOLF_ERROR_COND(cond, ...) \
527 if (cond) WOLF_ERROR(__VA_ARGS__);
530#define WOLF_DEBUG(...) \
533 char this_file[] = __FILE__; \
534 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
535 .debug("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
537#define WOLF_DEBUG_COND(cond, ...) \
540 if (cond) WOLF_DEBUG(__VA_ARGS__); \
543#define WOLF_DEBUG(...) \
547#define WOLF_DEBUG_COND(cond, ...) \
554#define WOLF_TRACE(...) \
557 char this_file[] = __FILE__; \
558 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
559 .trace("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
561#define WOLF_TRACE_COND(cond, ...) \
564 if (cond) WOLF_TRACE(__VA_ARGS__); \
567#define WOLF_TRACE(...) \
571#define WOLF_TRACE_COND(cond, ...) \
578#define WOLF_PROC_DEBUG(...) WOLF_DEBUG(__VA_ARGS__)
579#elif defined(WOLF_PRETTY_FUNCTION)
580#define WOLF_PROC_DEBUG(...) \
582 static_assert(std::is_base_of<ProcessorBase, typename std::remove_pointer<decltype(this)>::type>::value, \
583 "WOLF_PROC_DEBUG can only be used in processors"); \
585 bool called_from_derived = wolf::internal::isMemberFunctionOf(WOLF_PRETTY_FUNCTION, getType()); \
587 if (debug_verbose_level_ == "all" or (called_from_derived and (debug_verbose_level_ == "derived")) or \
588 (not called_from_derived and (debug_verbose_level_ == "base"))) \
591 char this_file[] = __FILE__; \
592 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
593 .debug("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
597#define WOLF_PROC_DEBUG(...) \
599 static_assert(std::is_base_of<ProcessorBase, typename std::remove_pointer<decltype(this)>::type>::value, \
600 "WOLF_PROC_DEBUG can only be used in processors"); \
602 if (debug_verbose_level_ != "none") \
604 char this_file[] = __FILE__; \
605 wolf::internal::WolfLogger::get(__INTERNAL_WOLF_MAIN_LOGGER_NAME_) \
606 .debug("[", basename(this_file), " L", __LINE__, "] ", __FUNCTION__, ": ", __VA_ARGS__); \
611#define WOLF_PROC_DEBUG_COND(cond, ...) \
612 if (cond) WOLF_PROC_DEBUG(__VA_ARGS__);
A thread-safer (?) Singleton implementation with argument forwarding.
Definition: singleton.h:33
Manager of named loggers, created on demand.
Definition: logging.h:313
bool exists(const std::string &name) const
Returns whether a logger exists (thread-safe)
Definition: logging.h:325
const Logger & getLogger(const std::string &name)
Returns a logger, creating it if it does not exist (thread-safe)
Definition: logging.h:333
~LoggerManager()=default
Destructor.
bool addLogger(const std::string &name)
Creates and adds a logger if it does not exist. Returns whether it was created.
Definition: logging.h:349
std::map< const std::string, const LoggerPtr > logger_map_
Loggers indexed by name.
Definition: logging.h:345
LoggerManager()=default
Default constructor.
std::mutex mut_
Mutex of the logger map.
Definition: logging.h:343
bool existsImpl(const std::string &name) const
Returns whether a logger exists (not thread-safe)
Definition: logging.h:359
Named logger, wrapping a spdlog color console logger.
Definition: logging.h:103
void error(Args &&... args) const
Logs an error message composed of the arguments.
Definition: logging.h:265
void info(Args &&... args) const
Logs an info message composed of the arguments.
Definition: logging.h:245
void operator=(Logger &)=delete
Copy assignment (deleted)
Logger(const std::string &name)
Constructor from the logger name.
Definition: logging.h:173
void set_pattern(const std::string &p)
Sets the spdlog pattern of the messages.
Definition: logging.h:303
void debug(Args &&... args) const
Logs a debug message composed of the arguments.
Definition: logging.h:275
bool set_async_queue(const std::size_t q_size)
Initializes the spdlog thread pool for asynchronous logging with a queue size (only if it is even)
Definition: logging.h:294
~Logger()
Destructor, dropping the spdlog logger.
Definition: logging.h:239
Logger(Logger &&)=delete
Move constructor (deleted)
const std::string log_name_
Name of the logger.
Definition: logging.h:168
void operator=(Logger &&)=delete
Move assignment (deleted)
Logger(Logger &)=delete
Copy constructor (deleted)
void trace(Args &&... args) const
Logs a trace message composed of the arguments.
Definition: logging.h:285
void warn(Args &&... args) const
Logs a warning message composed of the arguments.
Definition: logging.h:255
std::shared_ptr< spdlog::logger > console_
spdlog logger
Definition: logging.h:170
#define __INTERNAL_WOLF_MAIN_LOGGER_NAME_
Definition: logging.h:42
static constexpr auto repeated_brace
Definition: logging.h:44
std::unique_ptr< Logger > LoggerPtr
Definition: logging.h:308
bool isMemberFunctionOf(const std::string &_function_signature, const std::string &_class_name)
Checks if a function signature corresponds to a member function of the class wolf::_class_name.
Definition: logging.h:376
Definition: capture_base.h:23